from flask import Flask
from flask_apscheduler import APScheduler

from SysDB import init_db
from SysServers.sys_table_test1_servers import sys_server_table_test1_add, sys_server_amazon_order_get_all
from SysUtils import bnt_generate_verification_code

app = Flask(__name__)

init_db()
# 初始化任务调度器
scheduler = APScheduler()

# 配置任务调度器
app.config['SCHEDULER_API_ENABLED'] = True

misfire_grace_time = 900
gp_1 = '0,4,8,12,16,20,24,28,32,36,40,44,48,52,56'
gp_2 = '1,5,9,13,17,21,25,29,33,37,41,45,49,53,57'
gp_3 = '2,6,10,14,18,22,26,30,34,38,42,46,50,54,58'
gp_4 = '3,7,11,15,19,23,27,31,35,39,43,47,51,55,59'
gp_5 = '19,39,59'
gp_6 = '1,9,17,25,33,41,49,57'
gp_7 = '0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58'
gp_8 = '1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59'


# minute,seconds

@scheduler.task('cron', id='test1', minute=gp_7, misfire_grace_time=misfire_grace_time)
def sys_test1():
    items = sys_server_amazon_order_get_all()
    print(items)
    return str(len(items))


@scheduler.task('cron', id='test2', minute=gp_8, misfire_grace_time=misfire_grace_time)
def sys_test2():
    code = bnt_generate_verification_code(size=6)
    obj = sys_server_table_test1_add(nike_name=code)
    if obj:
        print(code)
        return 'ok'
    else:
        return 'false'


# 初始化任务调度器到 Flask 应用
scheduler.init_app(app)

if __name__ == '__main__':
    # 启动任务调度器
    scheduler.start()
    # 运行 Flask 应用
    # app.run(host='0.0.0.0', port=8089)
    app.run()
